package com.cj.controller;

import com.cj.po.Classes;
import com.cj.po.PageInfo;
import com.cj.po.Student;
import com.cj.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class ClassController {

    @Autowired
    private ClassService classService;

    @RequestMapping(value = "/findClass")
    public String findClass(Integer c_classid, String c_classname, String c_counsellor, Integer pageIndex, Integer pageSize, Model model){
        PageInfo<Classes> ci = classService.findPageInfo(c_classid,c_classname,c_counsellor,
                pageIndex,pageSize);
        model.addAttribute("ci",ci);
        model.addAttribute("c_name",c_classname);
        return "class_list";
    }

    @RequestMapping(value = "/findClassStudent")
    public String findClassStudent(Model model){
        List<Classes> cs = classService.getClassAndStudent();
        model.addAttribute("cs",cs);
        return "class_Studentlist";
    }

    /**
     * 导出Excel
     */
    @RequestMapping(value = "/exportclasslist", method = RequestMethod.POST)
    @ResponseBody
    public List<Classes> exportClass(){
        List<Classes> classList = classService.getAll();
        return classList;
    }

    @RequestMapping( "/findClassById")
    public String findClassById(Integer c_id,HttpSession session) {

        Classes c= classService.findClassById(c_id);
        session.setAttribute("c",c);
        return "class_edit";
    }

    @RequestMapping( "/deleteClass")
    @ResponseBody
    public String deleteClass(Integer c_id) {
        int c = classService.deleteClass(c_id);
        return "class_list";
    }

    @RequestMapping(value = "/addClass" ,method = RequestMethod.POST)
    @ResponseBody
    public String addClass(@RequestBody Classes classes) {
        int c = classService.addClass(classes);
        return "class_list";
    }

    @RequestMapping(value = "/updateClass" ,method = RequestMethod.POST )
    public String updateClass(Classes classes) {
        int c = classService.updateClass(classes);
        return "redirect:/findClass";
    }

}
